#!-*- encoding:utf8 -*-
import email
import mimetypes
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEImage import MIMEImage
import smtplib
import datetime
import math

from osmp.host.models import Host, Product, Host2Pro

def creatree(id, html, folder_url="", file_url="", flag = True, auth_pids = []):
    tree = Product.objects.filter(ppid = id)
    if id == -1:
        html += '<ul id="browser" class="filetree treeview">'
    else:
        html += "<ul>"
    for t in tree:
        #判断是否有子节点
        if not Product.objects.filter(ppid = t.id):
            h2p = Host2Pro.objects.filter(pid = t);
            if not t.id in auth_pids:
               return html +'</ul>'
            if h2p:
                if folder_url == "":
                    html += '<li><span class="folder">%s</span>\n' % (t.name)
                else:
                    html += '<li><span class="folder"><a href="%s%s" >%s</a></span>\n' % (folder_url, t.id, t.name)
                if flag:
                    html += "<ul>"
                    for e in h2p:
                        if file_url == "":
                            html += '<li><span class="file">%s</span></li>' % (e.hid.ip)
                        else:
                            html += '<li><span class="file"><a href="%s%s">%s</a></span></li>' % (file_url, e.hid.id, e.hid.ip)
                    html += "</ul>"
                html += "</li>"
            else:
                if folder_url == "":
                    html += '<li><span class="folder">%s</span></li>\n' %(t.name)
                else:
                    html += '<li><span class="folder"><a href="%s%s">%s</a></span></li>\n' %(folder_url, t.id, t.name)
        else:
            if folder_url == "":
                html += '<li><span class="folder">%s</span>' % (t.name)
            else:
                html += '<li><span class="folder"><a href="%s%s" >%s</a></span>' % (folder_url, t.id, t.name)
            html = creatree(t.id, html, folder_url, file_url, flag, auth_pids)
            html += "</li>\n"
    html += "</ul>\n"
    return html


def sendmail1(strTo, subject, htmlText):
    authInfo = {}
    server = "mail.baofeng.com"
    user = "yilan_report@baofeng.com"
    strFrom = user
#    user = "zhuchuanqi@baofeng.com"
    passwd = 'yilan@baofeng'
#    passwd = 'zhuchuanqi123'
#    设定root信息
    msgRoot = MIMEMultipart('related')
    msgRoot['From'] = strFrom
    msgRoot['To'] = strTo
    msgRoot['Subject'] = subject
    msgRoot.add_header("Date",  str(datetime.datetime.now()).split('.')[0])
    msgRoot.preamble = "this is a multi-part message in MIME format."
#    Encapsulate the plain and HTML versions of the message body in an
#    'alternative¡¯ part, so message agents can decide which they want to display.
    msgAlternative = MIMEMultipart('alternative')
    msgRoot.attach(msgAlternative)
#    设定纯文本信息
#    msgText = MIMEText(plainText, 'plain', 'utf-8')
#    msgAlternative.attach(msgText)
#    设定HTML信息
    msgText = MIMEText(htmlText, "html", "utf-8")
    msgAlternative.attach(msgText)
#    发送邮件
    smtp = smtplib.SMTP()
#    设定调试级别，依情况而定
    smtp.set_debuglevel(1)
    smtp.connect(server)
    smtp.login(user, passwd)
    smtp.sendmail(strFrom, strTo, msgRoot.as_string())
    smtp.quit()
    return "success"

def sendmail(to, subject, htmlText):

        strFrom = "panxiaodong@baofeng.com"
        strTo = to
        
        authInfo = {}
        server = "mail.baofeng.com"
        user = "yilan_report@baofeng.com"
        passwd = 'yilan@baofeng'

        # 设定root信息
        msgRoot = MIMEMultipart('related')
        msgRoot['Subject'] = subject
        #msgRoot['From'] = strFrom
        msgRoot['From'] = "osmp@baofeng.com"
        msgRoot['To'] = strTo
        msgRoot.add_header("Date",  str(datetime.datetime.now()).split('.')[0])

        msgRoot.preamble = "this is a multi-part message in MIME format."

        # Encapsulate the plain and HTML versions of the message body in an
        # 'alternative’ part, so message agents can decide which they want to display.
        msgAlternative = MIMEMultipart('alternative')
        msgRoot.attach(msgAlternative)


        #设定HTML信息
        htmlText = htmlText + "<br /><a href='http://60.28.110.228:1234/'>[OSMP]</a>"
        msgText = MIMEText(htmlText, "html", "utf-8")
        msgAlternative.attach(msgText)


       #发送邮件
        smtp = smtplib.SMTP()
       #设定调试级别，依情况而定
        smtp.set_debuglevel(1)
        smtp.connect(server)
        smtp.login(user, passwd)
        smtp.sendmail(strFrom, strTo, msgRoot.as_string())
        smtp.quit()
        return


def pagination(lst_all, page, num=24):
    lst_len = len(lst_all)
    start = (page -1) * 24
    end = page * 24
    if start <0: 
        start = 0
    if end > lst_len:
        end = lst_len
    lst = lst_all[start:end]
    page_num = int(math.ceil(lst_len/24.0))
    page_lst = []
    if page_num > 1:
        page_lst = xrange(1, page_num+1)
    return (lst, page_lst)

